package com.Jian.reggie.controller;

import com.Jian.reggie.common.BaseContext;
import com.Jian.reggie.common.R;
import com.Jian.reggie.impl.ShoppingCart;
import com.Jian.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: ShoppingCart
 * Package: com.Jian.reggie.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/29 9:02
 * @Version 1.0
 */
@RequestMapping("/shoppingCart")
@RestController
@Slf4j
public class ShoppingCartController {
  @Autowired
  private ShoppingCartService shoppingCartService;
  
  @GetMapping("/list")
  public R<List<ShoppingCart>> list() {
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ShoppingCart::getUserId, currentId);
    wrapper.orderByAsc(ShoppingCart::getCreateTime);
    List<ShoppingCart> list = shoppingCartService.list(wrapper);
    return R.success(list);
  }
  
  /**
   * 添加购物车
   */
  @PostMapping("/add")
  public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    shoppingCart.setUserId(BaseContext.getCurrentId());
    
    // 查询当前是套餐还是菜品
    if (shoppingCart.getDishId() != null) {
      // 如果是菜品，查询菜品信息
      wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
    } else {
      // 如果是套餐，查询套餐信息
      wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
    }
    // 查询当前菜品是否在购物车中，如果存在数量加一，如果不存在数量为一
    ShoppingCart one = shoppingCartService.getOne(wrapper);
    if (one != null) {
      one.setNumber(one.getNumber() + 1);
      shoppingCartService.updateById(one);
    } else {
      shoppingCart.setNumber(1);
      shoppingCartService.save(shoppingCart);
      one = shoppingCart;
    }
    
    return R.success(one);
  }
  
  /**
   * 清空购物车
   */
  @DeleteMapping("/clean")
  public R clean() {
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ShoppingCart::getUserId, currentId);
    shoppingCartService.remove(wrapper);
    return R.success("清空购物车成功");
  }
  
  /**
   * 删除购物车中的某一项
   */
  @PostMapping("/sub")
  public R sub(@RequestBody ShoppingCart shoppingCart) {
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    shoppingCart.setUserId(BaseContext.getCurrentId());
    
    // 查询当前是套餐还是菜品
    if (shoppingCart.getDishId() != null) {
      // 如果是菜品，查询菜品信息
      wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
    } else {
      // 如果是套餐，查询套餐信息
      wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
    }
    // 查询当前菜品是否在购物车中，如果存在数量减一，如果不存在数量为一
    ShoppingCart one = shoppingCartService.getOne(wrapper);
    if (one != null) {
      if (one.getNumber() > 1) {
        one.setNumber(one.getNumber() - 1);
        shoppingCartService.updateById(one);
        
      } else {
        one.setNumber(0);
        shoppingCartService.removeById(one.getId());
      }
    }
    
    return R.success(one);
  }
}
